package com.guigu.www.controller;

import com.guigu.www.pojo.Users;
import com.guigu.www.service.UsersService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class UsersController {
    @Autowired
    UsersService usersService;
    //登录
    @RequestMapping("/login.action")
    public ModelAndView userslogin(Users users,boolean rememberMe){
        System.out.println(rememberMe);
        //创建ModlAndView
        ModelAndView modelAndView=new ModelAndView();
        //创建subject
        Subject subject = SecurityUtils.getSubject();
        //获取令牌
        UsernamePasswordToken token=new
                UsernamePasswordToken(users.getLoginid(),users.getPassword());
        //是否选中记住密码
        token.setRememberMe(rememberMe);
        //subject设置sission
        subject.getSession().setAttribute("username",users.getLoginid());
        //判断是否是否登录成功
        try {
            subject.login(token);
            modelAndView.setViewName("index");
        }catch (Exception e){
            modelAndView.setViewName("login");
            modelAndView.addObject("msg",e.getMessage());
            System.out.println(e.getMessage());
            String msgs="Submitted credentials for token [org.apache.shiro.authc.UsernamePasswordToken - admin, rememberMe=false] did not match the expected credentials.";
            if(e.getMessage().equals(msgs)){
                modelAndView.addObject("msg","密码错误");
            }
        }

        return modelAndView;
    }

    //退出登录
    @RequestMapping("/loginout.action")
    public String loginout(){
        //获取subject
        Subject subject = SecurityUtils.getSubject();
        //退出
        subject.logout();

        return "login";
    }


    //session丢失的数据找回来
    @RequestMapping("/getsesstion.action")
    @ResponseBody
    public Map getsesstion(){
        //创建map
        Map map=new HashMap();
        //获取subject
        Subject subject = SecurityUtils.getSubject();
        //获取session
        Session session = subject.getSession();
        //获取值
        String username = (String) session.getAttribute("username");
        //判断
        if(StringUtils.isBlank(username)){
            //session丢失 获取subject的值
            username= (String) subject.getPrincipal();
            //赋值
            subject.getSession().setAttribute("username",username);
        }
        map.put("username",username);

        return map;
    }

    @RequestMapping("/queryLsByUsersD.action")
    @ResponseBody
    public List<Users> queryLsByUsersD(){
        return usersService.queryLsByUsersD();
    }
}
